package com.action;

import org.apache.struts2.ServletActionContext;

import com.dao.BaseDAO;
import com.entity.Finace;
import com.entity.Flat;
import com.entity.Room;
import com.entity.Stu;
import com.entity.Userinfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
public class RoomAction extends ActionSupport {
    Room data;
    BaseDAO dao = new BaseDAO();

	public String add() {
		dao.add(data);
		return show();
	}

	public String del() {
		dao.del(dao.findByID(Room.class, data.getId()));
		return show();
	}

	public String show() {
		ActionContext.getContext().put("all", dao.show("from Room"));
		return SUCCESS;
	}
	
	public String find() {
		String hql = "from Room where 1=1";
		List params = new ArrayList();
		if(!"".equals(data.getArea())){
			hql+=" and area=?";
			params.add(data.getArea());
		}
		if(!"".equals(data.getFlat())){
			hql+=" and flat=?";
			params.add(data.getFlat());
		}
		if(!"".equals(data.getRoomno())){
			hql+=" and roomno=?";
			params.add(data.getRoomno());
		}
		ActionContext.getContext().put("all", dao.find(hql, params.toArray()));
		return SUCCESS;
	}
	

	public String reg() {
		ActionContext.getContext().put("all", dao.show("from Room"));
		return "add";
	}
	public String equip() {
		Room room = (Room)dao.findByID(Room.class, data.getId());
		ActionContext.getContext().put("datas", room);
		return "show";
	}
	
	public String update() {
		Finace obj = new Finace();
		obj.setFlat(data.getFlat());
		obj.setRoomno(data.getRoomno());
		obj.setArea(data.getArea());
		obj.setEquip(ServletActionContext.getRequest().getParameter("equip"));
		dao.add(obj);
		
		ActionContext.getContext().put("all", dao.show("from Finace"));
		return "all";
	}

	public String delFinace() {
		dao.del(dao.findByID(Finace.class, data.getId()));
		ActionContext.getContext().put("all", dao.show("from Finace"));
		return "all";
	}
	
	public String showFinance(){
		ActionContext.getContext().put("all", dao.show("from Finace"));
		return "all";
	}
	
	
    public String edit(){
    	Object obj = dao.findByID(Room.class,data.getId());
    	ActionContext.getContext().put("record", obj);
    	return "modify";
    }
    

	public String save() {
		Room obj = (Room)dao.findByID(Room.class,data.getId());
		obj.setRoomno(data.getRoomno());
		obj.setNum(data.getNum());
		obj.setFreeNum(data.getFreeNum());
		dao.update(obj);
		return show();
	}
	
	public Room getData() {
		return data;
	}

	public void setData(Room data) {
		this.data = data;
	}
}
